package com.kara.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.kara.dao.ClazzDao;
import com.kara.dao.StudentDao;
import com.kara.entity.Clazz;
import com.kara.entity.Student;
import com.kara.service.StudentManager;

@Transactional
public class StudentManagerImpl implements StudentManager{

	@Autowired
	private StudentDao studentDao;
	
	
	@Autowired
	private ClazzDao clazzDao;
	
	
	@Override
	public Student addStudentToClazz(Clazz clazz, Student student) {
		Student savedStudent = studentDao.save(student);
		
		if (! clazz.getStudents().contains(savedStudent)){
			clazz.getStudents().add(savedStudent);
			clazzDao.save(clazz);	
		}
		
		return savedStudent;
	}


	@Override
	public boolean removeStudent(Clazz clazz, Student student) {
		if (clazz.getStudents().contains(student)){
			return clazz.getStudents().remove(student);
		}
		return false;
	}


	public Student getStudent(long studentId) {
		
		return studentDao.get(studentId);
	}

}
